//
//  BTPickerView.swift
//  btvideo
//
//  Created by mac on 2019/11/7.
//  Copyright © 2019 JarvanZhang. All rights reserved.
//

import UIKit
import SnapKit
import SwifterSwift

class ST_BTOncePickerView: UIView {
    
    static let height = 250+BTSafeAreaBottom
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = Color.init(hexString: "#F2F4F7")
        
        self.addSubview(top_view)
        top_view.snp.makeConstraints({ (make) in
            make.left.top.right.equalToSuperview()
            make.height.equalTo(44)
        })
        
        top_view.addSubview(confirm_picker_btn)
        confirm_picker_btn.snp.makeConstraints({ (make) in
            make.right.equalToSuperview().offset(-50)
            make.width.height.equalTo(44)
            make.top.equalToSuperview()
        })
        
        top_view.addSubview(content_label)
        content_label.snp.makeConstraints({ (make) in
            make.centerY.equalTo(confirm_picker_btn)
            make.centerX.equalToSuperview()
        })
        
        top_view.addSubview(cancel_picker_btn)
        cancel_picker_btn.snp.makeConstraints({ (make) in
            make.left.equalToSuperview().offset(50)
            make.width.height.equalTo(44)
            make.top.equalToSuperview()
        })
        
        self.addSubview(pickerView)
        pickerView.snp.makeConstraints({ (make) in
            make.top.equalTo(self.top_view.snp.bottom).offset(10)
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview().offset(-BTSafeAreaBottom)
        })
        
        let line = UIView()
        self.addSubview(line)
        line.backgroundColor = Color.init(hexString: "#E7ECF4")
        line.snp.makeConstraints({ (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(1)
        })
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    MARK: - Properties
    var pickerFont = UIFont.systemFont(ofSize: 17, weight: UIFont.Weight.medium)
    
    lazy var top_view:UIView = {
       let control = UIView()
        control.backgroundColor = Color.white
        return control
    }()
    
    lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.text = "请选择"
        control.textColor = Color.init(hexString: "#333333")
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.medium)
        return control
    }()
    
    lazy var confirm_picker_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setTitleForAllStates("完成")
        control.setTitleColorForAllStates(Color.init(hexString: "#333333")!)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold)
        control.addTarget(self, action: #selector(confirm_picker_action), for: UIControl.Event.touchUpInside)
        
        return control
    }()
    
    lazy var cancel_picker_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setTitleForAllStates("取消")
        control.setTitleColorForAllStates(Color.init(hexString: "#999999")!)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        control.addTarget(self, action: #selector(cancel_picker_action), for: UIControl.Event.touchUpInside)
        
        return control
    }()
    
    

    
    lazy var pickerView:UIPickerView = {
        let control = UIPickerView()
        control.delegate = self
        control.dataSource = self
        return control
    }()
    
    
    var option_array = [String]()
    
    fileprivate var selectedIdx = 0
    
//    MARK: - Event
    func bind(array:[String],normalIdx:Int){
        
        self.option_array = array
        
        UIView.animate(withDuration: 0) {
            
            self.pickerView.reloadAllComponents()
            
        } completion: { (f) in
            
            self.scroll_to_idx(normalIdx)
        }

        
        
    }
    
    func scroll_to_idx(_ index:Int){
        
        selectedIdx = index
        self.pickerView.selectRow(index, inComponent: 0, animated: true)
        
    }
    
    
    var confirm_pick_closure:((String)->())?
    
    var confirm_pick_index_closure:((Int)->())?
    
    @objc func confirm_picker_action(){
        
        if let t = self.option_array[safe:selectedIdx]{
            confirm_pick_closure?(t)
        }
        
        self.confirm_pick_index_closure?(selectedIdx)
        
        cancel_picker_action()
    }
    
    
    @objc func cancel_picker_action(){
        
        self.isHidden = true
        
    }
    
}

extension ST_BTOncePickerView:UIPickerViewDelegate,UIPickerViewDataSource{
    
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        
        return option_array.count
    }
    
//    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
//        return option_array[safe:row]
//    }
    
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        
       let label = (view as? UILabel) ?? UILabel()

       label.textColor = .black
       label.textAlignment = .center
       label.font = self.pickerFont

       // where data is an Array of String
       label.text = option_array[safe:row] ?? ""

       return label
     }
    
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        
        selectedIdx = row
        
    }
    
}
